<?php

namespace app\common\model\banxi\core;

use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\facade\Cache;
use think\model\concern\SoftDelete;

/**
 * 请假
 */
class CoreTrainerSalary extends BaseModel
{

    use SoftDelete;

    protected $name = 'banxi_core_trainer_salary';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'trainerId' => 'string',
        'antiSalary' => 'float',
        'baseSalary' => 'float',
        'practiceSalary' => 'float',
        'sixtySalary' => 'float',
        'thirtySalary' => 'float',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];
    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                        ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name,account');
            }
        ])->where($where)->paginate([
            'page' => $page,
            'list_rows' => $limit
        ]);
    }

    public function getInfo(array $where)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name,account');
            }
        ])->where($where)->find();
    }

    public function createSalary($salary)
    {
        $trainerId = $salary['trainerId'];
        $key = "CoreTrainerSalary:create:" . $trainerId;
        if (Cache::has($key)) {
            throw new ValidateException('创建薪资繁忙，请稍后重试');
        }
        Cache::set($key, 1, 30);
        try {
            $count = $this->where(['trainerId' => $trainerId])->count();
            if ($count > 0) {
                throw new ValidateException('教练薪资已创建，请不要重复创建');
            }
            $this->save($salary);
        } finally {
            Cache::delete($key);
        }
        return true;
    }

    public function del($ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function trainer()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }
}